package com.eight.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 用来绑定Rabbit的队列与交换机
 * @Author: Van.Planifolia
 * @CreateTime: 2023-03-08  17:31
 * @Version: 1.0
 */
@Configuration
public class RabbitConfig {
    /**
     * 绑定充值余额订单存活队列,存活时长为30分钟
     *
     * @return 充值余额订单存活队列
     */
    @Bean(value = "chargeOrderSurviveQueue")
    public Queue chargeOrderSurviveQueue() {
        Map<String, Object> param = new HashMap<>();
        param.put("x-message-ttl", 1000 * 5 * 60);
        param.put("x-dead-letter-exchange", "CODQE");
        return new Queue("chargeOrderSurviveQueue", true, false, false, param);
    }

    /**
     * 绑定充值余额订单超时死亡队列
     *
     * @return 充值余额订单超时死亡队列
     */
    @Bean(value = "chargeOrderDeadQueue")
    public Queue chargeOrderDeadQueue() {
        return new Queue("chargeOrderDeadQueue", true, false, false);
    }

    /**
     * 绑定租车余额订单存活队列,存活时长为30分钟
     *
     * @return 租车存活队列
     */
    @Bean(value = "carOrderSurviveQueue")
    public Queue carOrderSurviveQueue() {
        Map<String, Object> param = new HashMap<>();
        param.put("x-message-ttl", 1000 * 5 * 60);
        param.put("x-dead-letter-exchange", "CRODQE");
        return new Queue("carOrderSurviveQueue", true, false, false, param);
    }

    /**
     * 绑定租车余额订单超时死亡队列
     *
     * @return 充值余额订单超时死亡队列
     */
    @Bean(value = "carOrderDeadQueue")
    public Queue carOrderDeadQueue() {
        return new Queue("carOrderDeadQueue", true, false, false);
    }

    /**
     * 创建充值死信交换机
     *
     * @return 死信交换机
     */
    @Bean(value = "CODQE")
    public TopicExchange CODQE() {
        return new TopicExchange("CODQE", true, false);
    }

    /**
     * 创建租车死信交换机
     *
     * @return 死信交换机
     */
    @Bean(value = "CRODQE")
    public TopicExchange CRODQE() {
        return new TopicExchange("CRODQE", true, false);
    }

    /**
     * 创建一个普通路由交换机
     *
     * @return 普通路由交换机
     */
    @Bean(value = "SE")
    public TopicExchange SE() {
        return new TopicExchange("SE", true, false);
    }

    /**
     * 绑定付款死信
     *
     * @return 付款死信绑定
     */
    @Bean
    public Binding DxBinding1(Queue chargeOrderDeadQueue, TopicExchange CODQE) {
        return BindingBuilder.bind(chargeOrderDeadQueue).to(CODQE).with("charge.in");
    }

    /**
     * 绑定租车死信
     *
     * @return 租车死信绑定
     */
    @Bean
    public Binding DxBinding2(Queue carOrderDeadQueue, TopicExchange CRODQE) {
        return BindingBuilder.bind(carOrderDeadQueue).to(CRODQE).with("car.in");
    }

    /**
     * 将普通路由绑定到充值存活队列中
     *
     * @param chargeOrderSurviveQueue 充值存活队列
     * @param SE                      存活交换机
     * @return 绑定结果
     */
    @Bean
    public Binding SeBinding1(Queue chargeOrderSurviveQueue, TopicExchange SE) {
        return BindingBuilder.bind(chargeOrderSurviveQueue).to(SE).with("charge.*");
    }

    /**
     * 将普通路由绑定到充值存活队列中
     *
     * @param carOrderSurviveQueue 租车存活队列
     * @param SE                   存活交换机
     * @return 绑定结果
     */
    @Bean
    public Binding SeBinding2(Queue carOrderSurviveQueue, TopicExchange SE) {
        return BindingBuilder.bind(carOrderSurviveQueue).to(SE).with("car.*");
    }

}
